package com.ccrfid.rmc6.service.personnel;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.personnel.PersonnelEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelTypeEntity;
import com.ccrfid.rmc6.repository.personnel.PersonnelRepository;
import com.ccrfid.rmc6.repository.personnel.PersonnelTypeRepository;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class PersonnelTypeServiceImpl implements PersonnelTypeService {
    private static final Logger logger = LoggerFactory.getLogger(PersonnelTypeServiceImpl.class);

    @Autowired
    private PersonnelTypeRepository personnelTypeRepository;
    @Autowired
    private PersonnelRepository personnelRepository;
    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public PersonnelTypeEntity createPeronnelType(PersonnelTypeEntity personnelTypeEntity)throws AppException {
        if(personnelTypeRepository.existsByPersonnelTypeName(personnelTypeEntity.getPersonnelTypeName())){
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.type_already_hava.getCode(), CustomErrors.type_already_hava.getReason(), "");
        }
        return personnelTypeRepository.save(personnelTypeEntity);
    }

    @Override
    public PersonnelTypeEntity updatePersonnelType(Long id, PersonnelTypeEntity entity) throws AppException {
        if(!personnelTypeRepository.existsById(id)){
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        PersonnelTypeEntity personnelTypeEntity = personnelTypeRepository.getById(id);
        personnelTypeEntity.setPersonnelTypeName(entity.getPersonnelTypeName());
        personnelTypeEntity.setRemark(entity.getRemark());
        return personnelTypeRepository.save(personnelTypeEntity);
    }

    @Override
    public boolean deletePersonnelType(Long id) throws AppException {
        if(!personnelTypeRepository.existsById(id)){
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        personnelTypeRepository.deleteById(id);
        String typeId = "(" + id +  ")";
        personnelRepository.updatePersonnelCateGory(typeId);
        return true;
    }

    @Override
    public Page<PersonnelTypeEntity> getPersonnelType(SearchCriteria criteria, Pageable pageable) {
        return personnelTypeRepository.search(criteria, pageable);
    }
}
